import { Body, Controller, Delete, Get, Param, Post, Put, Query } from '@nestjs/common';
import { CategoryService } from './category.service';
import { ApiBearerAuth, ApiOperation, ApiQuery, ApiTags } from '@nestjs/swagger';
import { CategoryDto } from './category.dto';
import { CategoryEntity } from './category.entity';
import { ModuleSwaggerTags } from '~/constants/modules.constant';
import { Public } from '~/modules/auth/decorators/public.decorator';
import { ApiTenantHeader } from '~/common/decorators/api-header-tenant.decorator';

@ApiTags(ModuleSwaggerTags.CATEGORY)
@ApiBearerAuth()
@Controller('cms/category')
export class CategoryController {
  constructor(private readonly categoryService: CategoryService) {}

  @Post()
  @ApiOperation({ summary: '创建分类' })
  async create(@Body() dto: CategoryDto): Promise<CategoryEntity> {
    return await this.categoryService.create(dto);
  }

  @Public()
  @Get()
  @ApiTenantHeader()
	@ApiQuery({ name: 'name', required: false, description: '分类名称' })
	@ApiQuery({ name: 'code', required: false, description: '分类编码' })
  @ApiOperation({ summary: '获取所有分类' })
  async findAll(
    @Query('name') name?: string,
    @Query('code') code?: string
  ): Promise<CategoryEntity[]> {
    return await this.categoryService.searchByNameAndCode(name, code);
  }

  @Put(':id')
  @ApiOperation({ summary: '更新分类' })
  async update(@Body() dto: CategoryDto, @Param('id') id: string): Promise<CategoryEntity> {
    return await this.categoryService.update(Number(id), dto);
  }

  @Delete(':id')
  @ApiOperation({ summary: '删除分类（软删除）' })
  async delete(@Param('id') id: string): Promise<CategoryEntity> {
    return await this.categoryService.softDelete(Number(id));
  }
}
